package com.tiger.cloud.service.crm;

import com.tiger.cloud.model.crm.User;
import com.tiger.cloud.model.crm.UserAccount;
import com.tiger.cloud.model.crm.UserInfo;
import com.tiger.cloud.dao.crm.UserAccountDao;
import com.tiger.cloud.dao.crm.UserDao;
import com.tiger.cloud.enums.DeleteEnum;
import com.tiger.cloud.enums.UserStatusEnum;

import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tiger.cloud.common.mybatis.TigerBaseService;
import com.tiger.cloud.common.token.PasswordHashUtil;
import com.fasterxml.jackson.databind.node.DecimalNode;
import com.tiger.cloud.common.mybatis.MybatisTigerDao;

/**
 * 用户管理
 */
@Service("userService")
public class UserService extends TigerBaseService<User> implements IUserService{
    
    @Autowired
	private UserDao userDao;
    @Autowired
	private UserAccountDao userAccountDao;

	@Override
	public MybatisTigerDao<User> getEntityDao() {
		return userDao;
	}

	@Override
	@Transactional
	public User register(String loginId, String password) {
		User user=new User();
		user.setIsDelete(DeleteEnum.NOMAIL);
		user.setStatus(UserStatusEnum.AUDIT);
		user.setLoginId(loginId);
		try {
			user.setPassword(PasswordHashUtil.createHash(password));
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		}
		if (insert(user)) {
			UserAccount userAccount=new UserAccount();
			userAccount.setIntegral(0l);
			userAccount.setBalance(BigDecimal.ZERO);
			userAccount.setRecharge(BigDecimal.ZERO);
			if(userAccountDao.insert(userAccount)>0){
				return user;
			}
		}
		return null;
	}
    
}